package com.clayecloud.module_house.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.dialog.AddressDialog;
import com.clayecloud.lib_base.dialog.BaseDialog;
import com.clayecloud.lib_base.dialog.InputDialog;
import com.clayecloud.lib_base.dialog.SelectDialog;
import com.clayecloud.lib_common.apis.entity.house.ClientUserBindHouse;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.module_house.R;
import com.clayecloud.module_house.databinding.ActivityBindhouseBinding;
import com.clayecloud.module_house.viewmodels.ActivityBindHouseViewModel;
import com.clayecloud.module_house.viewmodels.ActivityHouseMainViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Route(path = "/house/add/index")
public class BindHouseActivity extends AppCompatActivity implements ToastAction {

    private ActivityBindhouseBinding activityBindhouseBinding;
    private ActivityBindHouseViewModel activityBindHouseViewModel;
    private Context context = this;

    private Integer userType = 0;
    private Integer idType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityBindhouseBinding = DataBindingUtil.setContentView(this, R.layout.activity_bindhouse);
        //获取数据模型
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        activityBindHouseViewModel = viewModelProvider.get(ActivityBindHouseViewModel.class);
        // 初始化函数进行初始化
        init();
        // 绑定监听事件
        initListener();
    }

    /**
     * 初始化函数
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        activityBindHouseViewModel.loadData();
    }

    /**
     * 注册监听事件
     */
    private void initListener() {
        activityBindhouseBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                back();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
        //选择地区点击监听
        activityBindhouseBinding.houseAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AddressDialog.Builder(context)
                        //.setTitle("选择地区")
                        // 设置默认省份
                        .setProvince(activityBindHouseViewModel.getmProvince())
                        // 设置默认城市（必须要先设置默认省份）
                        .setCity(activityBindHouseViewModel.getmCity())
                        // 不选择县级区域
                        //.setIgnoreArea()
                        .setListener((dialog, province, city, area) -> {
                            String address = province + "-" + city + "-" + area;
                            activityBindhouseBinding.houseAddress.setRightText(address);
                            activityBindhouseBinding.houseAddress.setRightTextColor(Color.parseColor("#303133"));
                        })
                        .show();
            }
        });
        // 小区输入点击事件
        activityBindhouseBinding.houseResidentialQuarters.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("小区名称")
                        .setHint(activityBindhouseBinding.houseResidentialQuarters.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityBindhouseBinding.houseResidentialQuarters.getRightText().equals(content)) {
                                activityBindhouseBinding.houseResidentialQuarters.setRightText(content);
                                activityBindhouseBinding.houseResidentialQuarters.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });
        // 小区门牌号
        activityBindhouseBinding.houseNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        .setTitle("门牌号")
                        .setHint(activityBindhouseBinding.houseNumber.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityBindhouseBinding.houseNumber.getRightText().equals(content)) {
                                activityBindhouseBinding.houseNumber.setRightText(content);
                                activityBindhouseBinding.houseNumber.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });
        // 绑定的姓名
        activityBindhouseBinding.username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        .setTitle("姓名")
                        .setHint(activityBindhouseBinding.username.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityBindhouseBinding.username.getRightText().equals(content)) {
                                activityBindhouseBinding.username.setRightText(content);
                                activityBindhouseBinding.username.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });
        // 手机号
        activityBindhouseBinding.userphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        .setTitle("手机号")
                        .setHint(activityBindhouseBinding.userphone.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityBindhouseBinding.userphone.getRightText().equals(content)) {
                                activityBindhouseBinding.userphone.setRightText(content);
                                activityBindhouseBinding.userphone.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });
        //选择类型
        activityBindhouseBinding.selectUsertype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SelectDialog.Builder(context)
                        .setTitle("选择你的身份")
                        .setList("业主", "家属")
                        // 设置单选模式
                        .setSingleSelect()
                        // 设置默认选中
                        .setSelect(0)
                        .setListener(new SelectDialog.OnListener<String>() {
                            @Override
                            public void onSelected(BaseDialog dialog, HashMap<Integer, String> data) {
                                Set keys = data.keySet();
                                userType = (Integer) keys.toArray()[0];
                                activityBindhouseBinding.selectUsertype.setRightText(data.get(keys.toArray()[0]));
                                activityBindhouseBinding.selectUsertype.setRightTextColor(Color.parseColor("#303133"));
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }
        });
        // 选择证件类型
        activityBindhouseBinding.selectIdtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SelectDialog.Builder(context)
                        .setTitle("选择你的身份")
                        .setList("身份证")
                        // 设置单选模式
                        .setSingleSelect()
                        // 设置默认选中
                        .setSelect(0)
                        .setListener(new SelectDialog.OnListener<String>() {
                            @Override
                            public void onSelected(BaseDialog dialog, HashMap<Integer, String> data) {
                                Set keys = data.keySet();
                                idType = (Integer) keys.toArray()[0];
                                activityBindhouseBinding.selectIdtype.setRightText(data.get(keys.toArray()[0]));
                                activityBindhouseBinding.selectIdtype.setRightTextColor(Color.parseColor("#303133"));

                            }
                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }
        });
        //身份证
        activityBindhouseBinding.idNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        .setTitle("身份证号码")
                        .setHint(activityBindhouseBinding.idNumber.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityBindhouseBinding.idNumber.getRightText().equals(content)) {
                                activityBindhouseBinding.idNumber.setRightText(content);
                                activityBindhouseBinding.idNumber.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });
        //提交内容
        activityBindhouseBinding.houseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String houseAddrass = (String) activityBindhouseBinding.houseAddress.getRightText();
                String houseResidentialQuarters = (String) activityBindhouseBinding.houseResidentialQuarters.getRightText();
                String houseNumber = (String) activityBindhouseBinding.houseNumber.getRightText();
                String username = (String) activityBindhouseBinding.username.getRightText();
                String userphone = (String) activityBindhouseBinding.userphone.getRightText();
                String idnumber = (String) activityBindhouseBinding.idNumber.getRightText();
                if("请选择地区".equals(houseAddrass) || "请填写小区".equals(houseResidentialQuarters) || "请填写门牌号".equals(houseNumber) ||
                        "请输入姓名".equals(username) || "请输入手机号".equals(userphone) || "请输入证件号码".equals(idnumber)){
                    toast("请检查提交内容，不能为空");
                    return;
                }
                ClientUserBindHouse clientUserBindHouse = new ClientUserBindHouse();
                clientUserBindHouse.setHouseAddrass(houseAddrass);
                clientUserBindHouse.setHouseResidentialQuarters(houseResidentialQuarters);
                clientUserBindHouse.setHouseNumber(houseNumber);
                clientUserBindHouse.setUserId(LoginUtil.getUserBaseinfo().getUserID());
                clientUserBindHouse.setUsername(username);
                clientUserBindHouse.setUserphone(userphone);
                clientUserBindHouse.setHouseUsertype(userType);
                clientUserBindHouse.setCertificatestype(idType);
                clientUserBindHouse.setUseridnumber(idnumber);
                activityBindHouseViewModel.submit(clientUserBindHouse);
            }
        });
        // 提交内容回调
        activityBindHouseViewModel.getSubmitSuccess().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer state) {
                if(state == 1){
                    toast("绑定成功");
                    back();
                }
                if(state == 2){
                    toast("绑定失败");
                    back();
                }
            }
        });
    }

    /**
     * 返回
     */
    private void back(){
        finish();
        overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
    }
}
